home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / rss / request.js < prev    next >
Text File  |  2010-02-02  |  4KB  |  186 lines

  1. function WiseStampCreateXMLHttpRequest()
  2. {
  3.     var xmlhttp = new XMLHttpRequest();
  4.  
  5.     return {
  6.         xmlhttp: xmlhttp,
  7.         readyState: 0,
  8.         responseXML: null,
  9.         responseText: "",
  10.         onsuccess: null,
  11.         onfailure: null,
  12.         url: "",
  13.         sent: "",
  14.         async: 1,
  15.         onsuccesscalled: false,
  16.  
  17.         makeOnReadyStateChangeHandler: function ()
  18.         {
  19.             var obj = this;
  20.             return function ()
  21.             {
  22.                 obj.readyState = obj.xmlhttp.readyState;
  23.  
  24.                 if (obj.xmlhttp.readyState == 4)
  25.                 {
  26.                     var status = '';
  27.  
  28.                     try
  29.                     {
  30.                         status = obj.xmlhttp.status;
  31.                     } catch(e)
  32.                     {
  33.                         obj.onerror(e);
  34.                         return;
  35.                     };
  36.  
  37.                     if (status == 200)
  38.                     {
  39.                         //WiseStampUtils.log("request.js :: makeOnReadyStateChangeHandler :: status = 200, obj.xmlhttp.responseText = " + obj.xmlhttp.responseText);
  40.                         
  41.                         obj.responseXML = obj.xmlhttp.responseXML;
  42.                         obj.responseText = obj.xmlhttp.responseText;
  43.  
  44.                         if (obj.onsuccess)
  45.                         {
  46.                             obj.onsuccesscalled = true;
  47.                             obj.onsuccess();
  48.                         };
  49.                     };
  50.                 };
  51.                 
  52.                 obj.onreadystatechange();
  53.             };
  54.         },
  55.  
  56.         makeOnErrorHandler: function ()
  57.         {
  58.             var obj = this;
  59.  
  60.             return function (e)
  61.             {
  62.                 obj.xmlhttp = new XMLHttpRequest();
  63.                 obj.xmlhttp.onerror = function ()
  64.                 {
  65.                     obj.onfailure();
  66.                 };
  67.                 obj.xmlhttp.onreadystatechange = obj.makeOnReadyStateChangeHandler();
  68.                 obj.xmlhttp.open(obj._open.method, obj._open.url, obj._open.mode);
  69.                 obj.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  70.                 obj.xmlhttp.send(obj.sent);
  71.             };
  72.         },
  73.  
  74.         onreadystatechange: function ()
  75.         {},
  76.  
  77.         open: function (method, url, mode)
  78.         {
  79.             this._open =
  80.             {
  81.                 method: method,
  82.                 url: url,
  83.                 mode: mode
  84.             };
  85.  
  86.             var obj = this;
  87.  
  88.             this.onerror = this.makeOnErrorHandler();
  89.             this.xmlhttp.onerror = this.makeOnErrorHandler();
  90.             this.xmlhttp.onreadystatechange = this.makeOnReadyStateChangeHandler();
  91.  
  92.             var ts = new Date;
  93.             if (url.indexOf('?') == -1)
  94.             {
  95.                 url = url + "?_ts=" + ts.getTime();
  96.             } else
  97.             {
  98.                 url = url + "&_ts=" + ts.getTime();
  99.             };
  100.  
  101.             this.url = url;
  102.             this.async = mode;
  103.  
  104.             this.xmlhttp.open(method, url, mode);
  105.             this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  106.         },
  107.  
  108.         send: function (data)
  109.         {
  110.             this.sent = data;
  111.             
  112.             this.xmlhttp.overrideMimeType("text/xml");
  113.             this.xmlhttp.send(data);
  114.  
  115.             // There's a difference in FF and IE behavior:
  116.             // IE calls onreadystatechange even foe non-async requests,
  117.             // while FF doesn't; thus, the following condition a 
  118.             // a workaround for FF behavior
  119.             if (!this.async && !this.onsuccesscalled && this.xmlhttp.readyState == 4)
  120.             {
  121.                 this.responseXML = this.xmlhttp.responseXML;
  122.                 this.responseText = this.xmlhttp.responseText;
  123.                 this.onsuccesscalled = true;
  124.                 if (this.onsuccess)
  125.                 {
  126.                     this.onsuccess();
  127.                 };
  128.             };
  129.         },
  130.  
  131.         handleError: function (callbacks)
  132.         {
  133.             // FireFox bug workaround;
  134.             // sometimes FF (up to 1.5.0.1) XMLHTTPRequest object 
  135.             // returns empty response body whatever the actual response was 
  136.             // (when calling from the FCKEditor event handler).
  137.             if (this.xmlhttp.responseText == "" && this.xmlhttp.responseXML == null)
  138.             {
  139.                 return false;
  140.             };
  141.  
  142.             if (!this.xmlhttp.responseXML)
  143.             {
  144.                 alert("Error; server responded with '" + this.xmlhttp.responseText + "'");
  145.                 return true;
  146.             };
  147.  
  148.             if (!this.xmlhttp.responseXML.documentElement)
  149.             {
  150.                 alert("Error; server responded with '" + this.xmlhttp.responseText + "'");
  151.                 return true;
  152.             };
  153.  
  154.             if (this.xmlhttp.responseXML.documentElement.tagName == "parsererror")
  155.             {
  156.                 alert("Error; server responded with '" + this.xmlhttp.responseText + "'");
  157.                 return true;
  158.             };
  159.  
  160.             var root = this.xmlhttp.responseXML.documentElement;
  161.             if (root.tagName == 'error')
  162.             {
  163.                 var handler = null;
  164.                 var code = root.getAttribute('code');
  165.                 var text = root.text ? root.text : root.textContent;
  166.  
  167.                 if (callbacks != null)
  168.                 {
  169.                     handler = callbacks[code];
  170.                 };
  171.  
  172.                 if (handler)
  173.                 {
  174.                     return handler(code, text);
  175.                 } else
  176.                 {
  177.                     var query_text = this.sent.replace(/&/g, "\n");
  178.                     alert("ERROR: " + code + " " + text + "\n" + query_text);
  179.                     return true;
  180.                 };
  181.             };
  182.  
  183.             return false;
  184.         }
  185.     }
  186. };